package com.newtouch.ainsurtech.client.demo;/**
 * Created by jovi on 28/09/2017.
 */


import com.newtouch.ainsurtech.entity.demo.PersonEntity;
import com.newtouch.ainsurtech.proxy.Response;
import com.newtouch.ainsurtech.proxy.demo.PersonDTO;
import com.newtouch.ainsurtech.proxy.demo.PersonPage;
import com.newtouch.ainsurtech.service.demo.PersonEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2016
 * All rights reserved. 2017-09-28 10:35
 * </p>
 *
 * @author MaoJiaWei
 * @version 1.0
 */
@Api(tags="API样例")
@RestController
@RequestMapping("/person")
public class PersonEntityController {

    @Autowired
    private PersonEntityService service;

    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param name 姓名
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @RequestMapping(method = RequestMethod.GET)
    public Response<Page<PersonPage>> list(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                           @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String name) {
        Page<PersonEntity> pages = service.pagingBy(name,page);
        Page<PersonPage> respS = pages.map(new Converter<PersonEntity, PersonPage>() {
            public PersonPage convert(PersonEntity source) {
                PersonPage resp = new PersonPage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "样例查询",httpMethod = "GET", notes = "暂无")
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public Response<PersonEntity> find( @ApiParam(required = true, name = "id", value = "查询对象主键") @PathVariable Long id) {
        return new Response<>(this.service.get(id));
    }

    /**
     * 添加对象
     * @param dto 对象
     * @return
     */
    @ApiOperation(value = "样例添加",httpMethod = "POST", notes = "暂无")
    @RequestMapping(method = RequestMethod.POST)
    public Response<Void> add( @ApiParam(required = true, name = "dto", value = "添加对象信息") @RequestBody PersonDTO dto) {
        PersonEntity entity = new PersonEntity();
        BeanUtils.copyProperties(dto,entity);
        this.service.save(entity);
        return new Response<>();
    }

    /**
     * 更新对象
     * @param id 更新对象的主键
     * @param dto 更新对象
     * @return
     */
    @ApiOperation(value = "样例更新",httpMethod = "PUT", notes = "暂无")
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public Response<Void> update( @ApiParam(required = true, name = "id", value = "更新对象主键") @PathVariable Long id,
                                  @ApiParam(required = true, name = "dto", value = "更新对象信息") @RequestBody PersonDTO dto) {
        PersonEntity entity = service.get(id);
        BeanUtils.copyProperties(dto,entity);
        this.service.update(entity);
        return new Response<>();
    }

    /**
     * 删除对象
     * @param id 删除对象的id
     * @return
     */
    @ApiOperation(value = "样例删除",httpMethod = "DELETE", notes = "暂无")
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public Response<Void> delete( @ApiParam(required = true, name = "id", value = "删除对象主键") @PathVariable Long id) {
        this.service.delete(id);
        return new Response<>();
    }
}
